const app=getApp();
const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:app.globalData.carts,
    disSubmit:true,
    name:'',
    work:'',
    address:'',
    tel:'',
    num:1,
    email:'',
    currentPro:'',
    flag:0,
    proType:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      currentPro: { "name": options.name, "code": options.code, "proId": options.proId },
      flag:options.flag
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      list: app.globalData.carts
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  addList: function (e) {
    util.cartNum(0, e.currentTarget.dataset.index);
  },
  reduceList: function (e) {
    util.cartNum(1, e.currentTarget.dataset.index);
  },
  check:function(e){
    var patt1 = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
    var patt2 = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if(e.target.id=="name"){
      this.setData({
        name:e.detail.value
      });
    } else if (e.target.id == "address") {
      this.setData({
        address: e.detail.value
      });
    } else if (e.target.id == "tel") {
      this.setData({
        tel: e.detail.value
      });
    } else if (e.target.id == "email") {
      this.setData({
        email: e.detail.value
      });
    } else if (e.target.id == "work") {
      this.setData({
        work: e.detail.value
      });
    }
    if (this.data.name != "" && this.data.address != "" && this.data.work != ""&&patt1.test(this.data.tel)&&patt2.test(this.data.email)){
      this.setData({
        disSubmit:false
      });
    }
  },
  submit:function(){
    this.setData({
      disSubmit:true
    });
    var orders=[];
    if(this.data.flag==1){
      if(this.data.proType==''||this.data.num==0){
        wx.showToast({
          title: '请选择规格或数量',
          icon: 'none',
          duration: 2000
        });
        this.setData({
          disSubmit: false
        });
        return ;
      }
      var temp={
        "name":this.data.currentPro.name,
        "proType": this.data.proType,
        "num":this.data.num,
        "proId":this.data.currentPro.proId,
        "code":this.data.currentPro.code
      };
      orders.push(temp);
    }else if(this.data.flag==0){
      for (let i = 0; i < this.data.list.length; i++) {
        this.data.list[i].code = app.globalData.cartFlag[i].split("||")[0];
      }
      orders=this.data.list;
    }
    if (orders.length>=1){
       var _this=this;
       wx.request({
         url: app.globalData.url + 'addOrder',
         method: 'POST',
         header: { 'content-type': 'application/x-www-form-urlencoded' },
         data: {
           order: JSON.stringify(orders),
           name: _this.data.name,
           work: _this.data.work,
           address: _this.data.address,
           tel: _this.data.tel,
           email: _this.data.email
         },
         success: function (res) {
            wx.showToast({
               title: res.data.msg,
               icon: 'success',
               duration: 2000,
               success:function(){
                 wx.navigateBack({
                   delta: 1
                 });
               }
             });
            _this.setData({
              disSubmit: false
            });
         }
       })
    }else{
      wx.showToast({
        title: "购物车没有订单",
        icon: 'none',
        duration: 2000
      })
    }
  },
  getType: function (e) {
    this.setData({
      proType: e.currentTarget.dataset.ml
    });
  },
  reduceNum: function () {
    if (this.data.num > 0) {
      this.setData({
        "num": this.data.num - 1
      })
    }
  },
  addNum: function () {
    this.setData({
      "num": this.data.num + 1
    })
  },
})